package com.fzy.common.bean;

import com.fzy.common.exception.BaseException;
import org.apache.commons.lang3.ClassUtils;
import org.springframework.util.FastByteArrayOutputStream;

import java.io.*;
import java.lang.reflect.Array;
import java.lang.reflect.Field;

/**
 * 对象工具类
 *
 * @author 冯赵杨
 * @create 2018/9/12
 */
public class ObjectUtil {
    private ObjectUtil() {
    }

    /**
     * 序列化<br>
     * 对象必须实现Serializable接口
     *
     * @param <T> 对象类型
     * @param obj 要被序列化的对象
     * @return 序列化后的字节码
     * @author 冯赵杨
     * @create 2018/09/07
     */
    public static <T> byte[] serialize(T obj) {
        if (null == obj | !(obj instanceof Serializable)) {
            return null;
        }

        FastByteArrayOutputStream byteOut = new FastByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(byteOut);
            oos.writeObject(obj);
            oos.flush();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return byteOut.toByteArray();
    }

    /**
     * 反序列化<br>
     * 对象必须实现Serializable接口
     *
     * @param <T>   对象类型
     * @param bytes 反序列化的字节码
     * @return 反序列化后的对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T unserialize(byte[] bytes) {
        ObjectInputStream ois;
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bais);
            return (T) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 对象比较
     * 1.null == null
     * 2.null != Object
     * 3.object.equals(other) is true,return true
     * 4.compare all Fields
     *
     * @param
     * @return
     * @author 冯赵杨
     * @create 2018/8/25
     */
    public static boolean equals(Object obj, Object other) {
        if (obj == null && other == null) {
            return true;
        }

        if (obj != null && other == null) {
            return false;
        }

        return compareNotNull(obj, other);
    }

    //非空比较
    private static boolean compareNotNull(Object obj, Object other) {
        if (obj.equals(other)) {
            return true;
        }
        Class type = obj.getClass();
        if (type != other.getClass()) {
            return false;
        }

        //简单类型
        if (ClassUtils.isPrimitiveOrWrapper(type)) {
            return comparePrimitiveType(obj, other);
        }
        //数组
        if (type.isArray()) {
            return compareArray(obj, other);
        }

        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                Object tmp1 = field.get(obj);
                Object tmp2 = field.get(other);
                if (!equals(tmp1, tmp2)) {
                    return false;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

        }
        return true;
    }

    private static boolean comparePrimitiveType(Object obj, Object other) {
        if (obj.getClass().isPrimitive()) {
            return obj == other;
        } else {
            return obj.equals(other);
        }
    }

    private static boolean compareArray(Object obj, Object other) {
        int len = Array.getLength(obj);
        if (len != Array.getLength(other)) {
            return false;
        }
        for (int i = 0; i < len; i++) {
            if (!equals(Array.get(obj, i), Array.get(other, i))) {
                return false;
            }
        }
        return true;
    }
}